###
#R Script to for Pooled Experimental Analyses
###

library(lfe)
library(stargazer)

load('experiments.pooled.RData')

###
#Politician Support by Issue Agreement and News Exposure
###

prime.all <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled)
prime.copartisan <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre +  factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(copartisan > .5)) 
prime.independent <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(copartisan==.5))
prime.outpartisan <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(copartisan < .5))

#Table 1 - Main Text
stargazer(prime.all,prime.copartisan,prime.independent,prime.outpartisan,keep=c('issue.treatment','issue.agree.pre'),dep.var.labels=c('All','Co-Partisans','Others'),covariate.labels=c('Article Treatment','Issue Agreement','Article Treatment*Issue Agreement'),notes=c('Issue Agreement measured prior to treatment','Model controls for pre-treatment views of Senator and Co-Partisanship'),star.cutoffs=c(0.05),notes.align='l',digits=2)

pr.stim <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(issue=='stimulus'))
pr.gun <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(issue=='gun'))
pr.chips <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(issue=='chips'))

#Table A11 - Appendix
stargazer(pr.stim,pr.gun,pr.chips,keep=c('issue.treatment','issue.agree.pre'),dep.var.labels=c('Stimulus','Gun Control','Chips'),covariate.labels=c('Article Treatment','Issue Agreement','Article Treatment*Issue Agreement'),notes=c('Issue Agreement measured prior to treatment','Model controls for pre-treatment views of Senator and Co-Partisanship'),star.cutoffs=c(0.05),notes.align='l',digits=2)

prime.all.c <- felm(legislator.post.scale ~ issue.treatment*issue.agree.scale.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled)
prime.copartisan.c <- felm(legislator.post.scale ~ issue.treatment*issue.agree.scale.pre +  factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(copartisan > .5)) 
prime.independent.c <- felm(legislator.post.scale ~ issue.treatment*issue.agree.scale.pre + legislator.support.pre | 0 | 0 | rid,data=experiments.pooled,subset=which(copartisan==.5))
prime.outpartisan.c <- felm(legislator.post.scale ~ issue.treatment*issue.agree.scale.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(copartisan < .5))

#Table A7 - Appendix
stargazer(prime.all.c,prime.copartisan.c,prime.independent.c,prime.outpartisan.c,keep=c('issue.treatment','issue.agree.scale.pre'),dep.var.labels=c('All','Co-Partisans','Others'),covariate.labels=c('Article Treatment','Issue Agreement','Article Treatment*Issue Agreement'),notes=c('Issue Agreement measured prior to treatment','Model controls for pre-treatment views of Senator and Co-Partisanship'),star.cutoffs=c(0.05),notes.align='l',digits=2)

prime.learnedposition <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(issue.accuracy.pre %in% c(-1,0)))
prime.knewposition <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(issue.accuracy.pre==1))

#Table 3 - Main Text
stargazer(prime.knewposition,prime.learnedposition,keep=c('issue.treatment','issue.agree.pre'),dep.var.labels=c('DK Position','Knew Position'),covariate.labels=c('Article Treatment','Issue Agreement','Article Treatment*Issue Agreement'),notes=c('Issue Agreement measured prior to treatment','Model controls for pre-treatment views of Senator and Co-Partisanship'),star.cutoffs=c(0.05),notes.align='l',digits=2)

###
#Issue Agreement with Politician by News Exposure
###

p.all <- felm(issue.agree.scale.post ~ issue.treatment + issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled)
p.co <- felm(issue.agree.scale.post ~ issue.treatment + issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(copartisan > .5))
p.ind <- felm(issue.agree.scale.post ~ issue.treatment + issue.agree.pre + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(copartisan==.5))
p.out <- felm(issue.agree.scale.post ~ issue.treatment + issue.agree.pre +factor(copartisan) +factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(copartisan < .5))

#Table 2 - Main Text
stargazer(p.all,p.co,p.ind,p.out,keep=c('issue.treatment'),covariate.labels=c('Article Treatment'),star.cutoffs=c(0.05),notes.align='l',digits=2)

p.stim <- felm(issue.agree.scale.post ~ issue.treatment + issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(issue=='stimulus'))
p.gun <- felm(issue.agree.scale.post ~ issue.treatment + issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(issue=='gun'))
p.chips <- felm(issue.agree.scale.post ~ issue.treatment + issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(issue=='chips'))

#Table A12 - Appendix
stargazer(p.stim,p.gun,p.chips,keep=c('issue.treatment'),covariate.labels=c('Article Treatment'),star.cutoffs=c(0.05),notes.align='l',digits=2)

prime.knowparty <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(party.accurate==1))
prime.dkparty <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(party.accurate==0))

#Table A6 - Appendix
stargazer(prime.knowparty,prime.dkparty,keep=c('issue.treatment','issue.agree.pre'),dep.var.labels=c('DK Senator Party','Knew Senator Party'),covariate.labels=c('Article Treatment','Issue Agreement','Article Treatment*Issue Agreement'),notes=c('Issue Agreement measured prior to treatment','Model controls for pre-treatment views of Senator and Co-Partisanship'),star.cutoffs=c(0.05),notes.align='l',digits=2)

###
#Robustness - People Who Disagreed With Party's Position Subset Analysis
###

experiments.pooled$type <- NA

experiments.pooled$type[which(experiments.pooled$issue.support.pre==1 & experiments.pooled$party_text=="Republican" & experiments.pooled$issue=="stimulus")] <- 'party-inconsistent'
experiments.pooled$type[which(experiments.pooled$issue.support.pre==0 & experiments.pooled$party_text=="Republican" & experiments.pooled$issue=="stimulus")] <- 'party-consistent'
experiments.pooled$type[which(experiments.pooled$issue.support.pre==1 & experiments.pooled$party_text=="Democrat" & experiments.pooled$issue=="stimulus")] <- 'party-consistent'
experiments.pooled$type[which(experiments.pooled$issue.support.pre==0 & experiments.pooled$party_text=="Democrat" & experiments.pooled$issue=="stimulus")] <- 'party-inconsistent'

experiments.pooled$type[which(experiments.pooled$issue.support.pre==1 & experiments.pooled$party_text=="Republican" & experiments.pooled$issue=="chips")] <- 'party-inconsistent'
experiments.pooled$type[which(experiments.pooled$issue.support.pre==0 & experiments.pooled$party_text=="Republican" & experiments.pooled$issue=="chips")] <- 'party-consistent'
experiments.pooled$type[which(experiments.pooled$issue.support.pre==1 & experiments.pooled$party_text=="Democrat" & experiments.pooled$issue=="chips")] <- 'party-consistent'
experiments.pooled$type[which(experiments.pooled$issue.support.pre==0 & experiments.pooled$party_text=="Democrat" & experiments.pooled$issue=="chips")] <- 'party-inconsistent'

experiments.pooled$type[which(experiments.pooled$issue.support.pre==1 & experiments.pooled$party_text=="Republican" & experiments.pooled$issue=="gun")] <- 'party-inconsistent'
experiments.pooled$type[which(experiments.pooled$issue.support.pre==0 & experiments.pooled$party_text=="Republican" & experiments.pooled$issue=="gun")] <- 'party-consistent'
experiments.pooled$type[which(experiments.pooled$issue.support.pre==1 & experiments.pooled$party_text=="Democrat" & experiments.pooled$issue=="gun")] <- 'party-consistent'
experiments.pooled$type[which(experiments.pooled$issue.support.pre==0 & experiments.pooled$party_text=="Democrat" & experiments.pooled$issue=="gun")] <- 'party-inconsistent'

prime.inc  <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(type=='party-inconsistent'))
persuasion.inc <- felm(issue.agree.scale.post.party ~ issue.treatment + issue.agree.pre + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled,subset=which(type=='party-inconsistent'))

#Table A10 - Appendix
stargazer(prime.inc,keep=c('issue.treatment','issue.agree.pre'),covariate.labels=c('Article Treatment','Issue Agreement','Article Treatment*Issue Agreement'),star.cutoffs=c(0.05),notes.align='l',digits=2)

#Table A9 - Appendix
stargazer(persuasion.inc,keep=c('issue.treatment'),covariate.labels=c('Article Treatment'),star.cutoffs=c(0.05),notes.align='l',digits=2)

###
#Robustness - Vote Choice Only Analysis 
###

prime.vote <- felm(I(vote.post==1) ~ issue.treatment*issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled)

#Table A8 - Appendix
stargazer(prime.vote,keep=c('issue.treatment','issue.agree.pre'),covariate.labels=c('Article Treatment','Issue Agreement','Article Treatment*Issue Agreement'),star.cutoffs=c(0.05),notes.align='l',digits=2)

###
#Robustness - Interactive Model with multiple Considerations
###

multiple.interaction.model <- felm(legislator.post.scale ~ issue.treatment*issue.agree.pre + issue.treatment*copartisan + issue.treatment*ideological.alignment + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled)

#Table A13 - Appendix
stargazer(multiple.interaction.model,keep=c('issue.treatment','issue.agree.pre','ideological.alignment','copartisan'),star.cutoffs=c(0.05),notes.align='l',digits=2,covariate.labels=c('Article Treatment','Issue Agreement','Co-Partisan','Ideological Alignment','Article Treatment*Issue Agreement','Article Treatment*Co-Partisan','Article Treatment*Ideological Alignment'))

###
#Article Exposure and Awareness of Legislator Position
###

correct.attentioncheck <- felm(correct.post ~ issue.treatment + issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled)
wrong.attentioncheck <- felm(wrong.post ~ issue.treatment + issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled)
dk.attentioncheck <- felm(dk.post ~ issue.treatment + issue.agree.pre + factor(copartisan) + factor(legislator.support.pre) | 0 | 0 | rid,data=experiments.pooled)

#Table A5 - Appendix
stargazer(correct.attentioncheck,dk.attentioncheck,wrong.attentioncheck,keep=c('issue.treatment'),dep.var.labels=c('Correct','Wrong','DK'),covariate.labels=c('Article Treatment'),star.cutoffs=c(0.05),notes.align='l',digits=2)

###
#Generating Outcome Scales (Appendix Tables A3 / A4)
###

#Example of code below used to produce the outcome scales.

#For the politician approval measure it is re-oriented so that higher values reflect greater support for the politician.

#For the issue approval measure this is then re-oriented for each respondent-politician pair to indicate greater agreement with the politician's issue stance

#Politician Approval - Study 1
summary(prcomp(experiments.pooled[which(experiments.pooled$issue=="stimulus"),c('approve.post','vote.post','therm.post')],center=TRUE,scale=TRUE))
prcomp(experiments.pooled[which(experiments.pooled$issue=="stimulus"),c('approve.post','vote.post','therm.post')],center=TRUE,scale=TRUE)$rotation

#Stimulus Issue Approval - Study 1
summary(prcomp(experiments.pooled[which(experiments.pooled$issue=="stimulus"),c('approve.post','vote.post','therm.post')],center=TRUE,scale=TRUE))
prcomp(experiments.pooled[which(experiments.pooled$issue=="stimulus"),c('approve.post','vote.post','therm.post')],center=TRUE,scale=TRUE)$rotation
